package com.lsf.dunlian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.lsf.dunlian.R;
import com.lsf.dunlian.adapter.MapSearchAddressRecyclerViewAdapter;
import com.lsf.dunlian.callback.ListClickCallback;
import com.lsf.dunlian.config.PreferenceUtil;
import com.lsf.dunlian.constant.Constant;
import com.lsf.dunlian.databinding.ActivityMapSearchAddressBinding;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static android.os.Build.ID;

public class MapSearchAddressActivity extends Activity implements PoiSearch.OnPoiSearchListener, Inputtips.InputtipsListener, ListClickCallback {

    private ActivityMapSearchAddressBinding binding;
    private MapSearchAddressRecyclerViewAdapter addressRecyclerViewAdapter;
    private List<Tip> tips = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseUtils.setStatusBar(this);
        binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_map_search_address, null, false);
        setContentView(binding.getRoot());

        addressRecyclerViewAdapter = new MapSearchAddressRecyclerViewAdapter(tips, MapSearchAddressActivity.this);
        addressRecyclerViewAdapter.setClickCallback(this);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(MapSearchAddressActivity.this));
        binding.recyclerView.setAdapter(addressRecyclerViewAdapter);

        binding.map.onCreate(savedInstanceState);
        AMap map = binding.map.getMap();
        MyLocationStyle myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        map.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        map.getUiSettings().setMyLocationButtonEnabled(true);
        map.setMyLocationEnabled(true);

        map.moveCamera(CameraUpdateFactory.zoomTo(16));

        binding.address.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtils.isEmpty(s)) return;
                PoiSearch poiSearch = new PoiSearch(MapSearchAddressActivity.this, null);
                poiSearch.setOnPoiSearchListener(MapSearchAddressActivity.this);
//        poiSearch.searchPOIIdAsyn(ID);// 异步搜索
                //第二个参数传入null或者“”代表在全国进行检索，否则按照传入的city进行检索
                InputtipsQuery inputquery = new InputtipsQuery(s.toString(), PreferenceUtil.getSharedPreference(Constant.CITYCODE));
                inputquery.setCityLimit(true);//限制在当前城市
                Inputtips inputTips = new Inputtips(MapSearchAddressActivity.this, inputquery);
                inputTips.setInputtipsListener(MapSearchAddressActivity.this);
                inputTips.requestInputtipsAsyn();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        String a = "111" + 1;
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {
        String a = "111" + 1;
    }

    @Override
    public void onGetInputtips(List<Tip> list, int i) {
        if (list != null && list.size() > 0) {
            tips.clear();
            tips.addAll(list);
            addressRecyclerViewAdapter.notifyDataSetChanged();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        binding.map.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        binding.map.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        binding.map.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        binding.map.onSaveInstanceState(outState);
    }

    @Override
    public void click(String val) {
        //数据是使用Intent返回
        Intent intent = new Intent();
        //把返回数据存入Intent
        intent.putExtra("result", val);
        //设置返回数据
        this.setResult(RESULT_OK, intent);
        //关闭Activity
        this.finish();
    }
}